/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.persistence.facade;

import be.ugent.medirem.persistence.entity.Location;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.values.LocationValues;
import be.ugent.medirem.persistence.exception.DaoException;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.util.EntityDetailsToEntity;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Thomas Roelens
 */
@Stateless
public class LocationDaoBean implements LocationDaoLocal {
    @PersistenceContext
    private EntityManager em;



    public Long create(LocationValues location) throws DaoException {
        try {
            Location l = EntityDetailsToEntity.detailsToLocation(location);
            em.persist(l);
            return l.getId();
        } catch (Exception e) {
            throw new DaoException("Failed to persist location : " + location.toString(), e);
        }
    }

    public LocationDetails read(Long id) throws EntityNotFoundException, DaoException {
        try {
            Location l = em.find(Location.class, id);
            if (l == null) {
                throw new EntityNotFoundException("No location found with id " + id);
            } else {
                return EntityDetailsToEntity.locationToDetails(l);
            }
        } catch (Exception e) {
            throw new DaoException("EntityManager failed when searching for location with id " + id, e);
        }

    }

    public Long update(Long id, LocationValues location) throws EntityNotFoundException, DaoException {
        Long newId = id;
        try {
            Location l = em.find(Location.class, id);
            if (l == null) {
                throw new EntityNotFoundException("No location found with id " + id);
            } else {
                if(location.getCountry() != null) {
                    l.setCountry(location.getCountry());
                }
                if(location.getEmail() != null) {
                    l.setEmail(location.getEmail());
                }
                if(location.getNumber() != 0) {
                    l.setNumber(location.getNumber());
                }
                if(location.getPhoneNumber() != null) {
                    l.setPhonenr(location.getPhoneNumber());
                }
                if(location.getStreet() != null) {
                    l.setStreet(location.getStreet());
                }
                if(location.getTown() != null) {
                    l.setTown(location.getTown());
                }
                if(location.getZipcode() != 0) {
                    l.setZipcode(location.getZipcode());
                }
                em.merge(l);
                newId = l.getId();
            }
        } catch (Exception e) {
            throw new DaoException("EntityManager failed when searching for location with id " + id, e);
        }

        return newId;
    }

    public void persist(Object object) {
        em.persist(object);
    }
}
